use strict;
use Digest::SHA1 qw(sha1_base64);
use DB_File;

my $group_file="test.group";
my $passwd_file="test.passwd";
my %group;

package passwd;
sub check {
	my $uid = shift;
	my $pass = shift;
	
	$pass = Digest::SHA1::sha1_base64($pass);

	my $db_uid;
	my $db_pass;

	open FD, "<".$passwd_file or die $!;
	while (my $l = <FD>) {
		($db_uid, $db_pass) = split(/\|/, $l);
		chomp $db_pass;
		if(($db_uid eq $uid) && ($db_pass eq $pass)) {
			close FD;
			return 1;
		}
	}	
	close FD;
	return 0;
}

sub add {
	my $uid = shift;
	my $pass = shift;
	
	$pass = Digest::SHA1::sha1_base64($pass);

	open FD, ">>".$passwd_file or die $!;
	print FD $uid."|".$pass."\n";
	close FD;
}	

sub del {
	my $uid = shift;
	#TODO	
}
sub dump {
	tie(%group, "DB_File", $group_file);
	foreach my $u (keys %group) {
		print $u."->".$group{$u}."\n";
	}
	untie(%group);
}

sub dump_hash {
	tie(%group, "DB_File", $group_file);
	return %group;
	untie(%group);
}

sub group {
	my $uid = shift;
	tie(%group, "DB_File", $group_file);	
	my $gid = $group{$uid};
	if(defined($gid)) {
		return $gid;	
	} else {
		return ();
	}
	untie(%group);

}

sub set_group {
	my $uid = shift;
	my $gid = shift;

	tie(%group, "DB_File", $group_file);
	$group{$uid} = $gid;
	untie(%group);
}
1;
